define (require, exports, module) ->

  apiObj = require 'mods/home/home_api'
  # 引入样式
  require 'mods/home/home_css.css'
  require 'libs/swiper/swiper.min.css'
  require 'libs/swiper/swiper.min.js'

  # 引入获取终端类型
  {getDeviceType} = require "js/device_type"

  # 定义组件
  module.exports = Vue.extend

    # 变量声明
    data: () ->
      # 是否加载完成
      loadedSucc: false
      # 加载失败提示语
      loadedFail: ''
      # 获取设备类型
      device: getDeviceType()
      # 轮播图
      homeSwiper: {}
      # 轮播图图片
      swiperItems: [
        {
          'img_addr':'http://my.gf.com.cn/pfs/mods/home/img/banner1.jpg'
        }, {
          'img_addr':'http://my.gf.com.cn/pfs/mods/home/img/banner2.jpg'
        },
      ]

      ydSwiperItems: [
        {
          'img_addr':'http://my.gf.com.cn/pfs/mods/home/img/banner_sj1.jpg'
        }, {
          'img_addr':'http://my.gf.com.cn/pfs/mods/home/img/banner_sj2.jpg'
        },
      ]

    # 组件模板
    template: require 'mods/home/home_tpl.js'

    # 组件方法
    methods:

      # 判断是否在显示时间内
      getGgStatu:(d1, d2)->

        # 默认false
        statu = false

        # 获取当前日期
        curdate = new Date()
        y = curdate.getFullYear()
        m = curdate.getMonth() + 1
        d = curdate.getDate()
        if m < 10
          m = '0' + m
        d0 = y + '-' + m + '-' + d

        # 将字符串日期转换为可比较
        time0 = new Date(d0?.replace("-", "/").replace("-", "/"));
        time1 = new Date(d1?.replace("-", "/").replace("-", "/"));
        time2 = new Date(d2?.replace("-", "/").replace("-", "/"));

        # 当前日期在开始和结束日期之间
        if time0 >= time1 and time0 <= time2
          statu = true

        return statu

      # 设置轮播图不同情况下的样式
      setBannerStyle: () ->
        self = @

        # 只有一张轮播图
        if 1 is self.swiperItems?.length
          # 轮播插件初始化
          self.homeSwiper = new Swiper '.swiper-container',
            centeredSlides: true
            autoplayDisableOnInteraction: false
        else
          # PC端
          if 'PC' is self.device
            self.homeSwiper = new Swiper '.swiper-container',

              # 图片居中
              centeredSlides: true
              autoplayDisableOnInteraction: false

              # 自动播放
              # loop: true
              autoplay: 3000

              # 小圆点
              pagination: '.swiper-pagination'
              paginationClickable: true

              # 上一张下一张按钮
              nextButton: '.swiper-button-next'
              prevButton: '.swiper-button-prev'

              effect: 'fade'

          # 移动端
          else
            # 隐藏首页弹出广告
            # $(".c-pop").css "display",'none'

            # 轮播插件初始化
            self.homeSwiper = new Swiper '.swiper-container',
              # 图片居中
              centeredSlides: true
              autoplayDisableOnInteraction: false

              # 自动播放
              # loop: true
              autoplay: 2500

              # 小圆点
              pagination: '.swiper-pagination'
              paginationClickable: true

              effect: 'fade'

      # 加载成功
      loadedSuccFunc: () ->
        self = @
        self.$nextTick () ->
          # 加载成功标识
          self.loadedSucc = true

      # 获取轮播广告的内容
      getPopAds: () ->
        self = @
        # 查询广告的参数
        params =
          pageNum: 1
          pageSize: 10

        return apiObj.getPopAds params
          .then (res) ->
            itemList = res?.body?.data
            for item in itemList
              statu = self.getGgStatu item.begin_date, item.end_date
              if 'banner_pc' is item.gg_type && statu
                self.swiperItems.push item
              else if 'banner_mb' is item.gg_type && statu
                self.ydSwiperItems.push item

          .catch (err) ->
            console.log err

    # 创建时触发
    created: () ->
      self = @

    # 路由切换时销毁swiper组件
    beforeRouteLeave: (to, from, next) ->
      self = @
      # destroy(deleteInstance, cleanupStyles)
      if self?.homeSwiper?.destroy
        self.homeSwiper?.destroy(false, true)
      return next true

    # 引入组件
    components:
      'c-zdymk': require 'mods/zdymk/zdymk_mod'
      'c-pop-ads': require 'mods/sygg/sygg_mod'

    # 计算属性
    computed:
      # 加载中的样式
      loaddingClass: () ->
        self = @
        if self.loadedSucc
          return 'c-hidden'
        if self.loadedFail
          return 'c-hidden'
        return ''

      # 加载成功的样式
      loadedSuccClass: () ->
        self = @
        return if self.loadedSucc then '' else 'c-vhidden'

      # 加载失败的样式
      loadedFailClass: () ->
        self = @
        return if self.loadedFail then '' else 'c-hidden'

    # 组件初始化执行
    mounted: () ->
      self = @

      # 获取轮播图数据
      # self.getPopAds()
      #   .then (res) ->
      #     self.setBannerStyle()
      #     # 加载成功 - 处理函数
      #     return self.loadedSuccFunc()
      #   .catch (err) ->
      #     console.error 'err:',err

      self.setBannerStyle()
      return self.loadedSuccFunc()